// use log::info;
use poker_lib::{PokerCard, PokerCards, Suit};

#[derive(Debug)]
pub struct Seat {
    pub id: usize,
    // None表示还没叫牌
    pub bid: Option<i16>,
    // 是否盲叫nil
    pub blind_nil: bool,
    // None表示本轮还没出牌
    pub out_card: Option<PokerCard>,
    pub tricks: i16,
    // 手牌，随着出牌逐渐减少
    pub cards: PokerCards,
    pub cards_origin:PokerCards,
    // 本轮备选牌
    pub cards_can: PokerCards,
    //丢失a和k的统计
    pub num_of_ak_lost_to_rival: i16,
    //根据每个轮的trick结果记录玩家是否空缺情况
    pub voided_suits: Vec<Suit>,
}

impl Seat {
    pub fn new(seatid: usize) -> Self {
        Self {
            id: seatid,
            bid: None,
            blind_nil: false,
            out_card: None,
            tricks: 0,
            cards: PokerCards::new(),
            cards_can: PokerCards::new(),
            cards_origin:PokerCards::new(),
            num_of_ak_lost_to_rival: 0,
            voided_suits: vec![],
        }
    }

    pub fn reset(&mut self) {
        self.bid = None;
        self.out_card = None;
        self.tricks = 0;
        self.cards.reset();
        self.cards_can.reset();
        self.cards_origin.reset();
        self.num_of_ak_lost_to_rival = 0;
        self.voided_suits.clear()
    }

    ///每轮结束后 记录下空缺和关键丢牌的状态
    pub fn update_stat_after_turn(
        &mut self,
        mine: &PokerCard,
        leading: &PokerCard,
        win: PokerCard,
    ) {
        //判断是否缺leading对应的花色
        if mine.suit != leading.suit {
            if !self.voided_suits.contains(&leading.suit) {
                self.voided_suits.push(leading.suit);
            }
        }
        //丢A 和 K
        let rank = mine.get_number();
        if (rank == 14 || rank == 13)
            && mine != &win
            && mine.suit == leading.suit
            && win.is_trump_card()
        {
            self.num_of_ak_lost_to_rival += 1;
        }
    }
    //黑桃和大小王算同一个花色
    pub fn is_suit_voided(&self, suit: Suit) -> bool {
        if suit == Suit::Spade || suit == Suit::Joker {
            return self.voided_suits.contains(&Suit::Spade)
                && self.voided_suits.contains(&Suit::Joker);
        }
        self.voided_suits.contains(&suit)
    }
    // 没主牌的判断
    pub fn has_no_trump_card(&self) -> bool {
        self.voided_suits.contains(&Suit::Spade) && self.voided_suits.contains(&Suit::Joker)
    }

    pub fn play_card(&mut self, card: PokerCard) {
        self.cards.remove(card);
        self.cards_can.clear();
        self.out_card = None;
    }
}
